﻿Imports System.Xml.Serialization
Imports Ext.Net
Imports Ext.Net.MVC
Imports DailyReport.DailyReportCommon

Namespace DailyReport
    Public Class DayController
        Inherits ControllerBase

        'Function CheckList(ByVal userid As Integer, ByVal checkdate As Integer) As ActionResult
        '    Return View()
        'End Function
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Authorize()>
        Function CheckList() As ActionResult
            '传2个参数去页面
            ViewData("UserID") = CurrentUserID  '获取当前用户ID，从ColtrollerBase
            ViewData("CheckDate") = Now '默认日期

            Return View()
        End Function

        <Authorize()>
        Function CheckDetail(ByVal userid As String, ByVal checkdate As Date) As ActionResult
            ViewData("UserID") = userid
            ViewData("CheckDate") = checkdate

            Return View()
        End Function

        <Authorize()>
        Function UserCheckReport(ByVal userid As String, ByVal username As String, ByVal rangestart As Date, ByVal rangeend As Date) As ActionResult
            Dim ucrm As New UserCheckReportModel
            Dim ucri As UserCheckReportItem

            ViewData("UserID") = userid
            ViewData("UserName") = username

            '计算开始日期区间
            Dim startDate As Date = rangestart
            Dim endDate As Date = rangeend

            'Select Case range
            '    Case 1 '月
            '        startDate = New Date(Now.Year, Now.Month, 1)    '本月1号
            '        endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)  '本月最后一日，先+1月，然后-1日就可以得到本月最后一日
            '    Case 2 '季
            '        startDate = New Date(Now.Year, Now.Month, 1)
            '        endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
            '    Case 3 '年
            '        startDate = New Date(Now.Year, Now.Month, 1)
            '        endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
            'End Select

            '第一次先从数据库取出原始数据，备用
            Dim ocl As New List(Of Check) '定义一个新的Check Model列表
            Using re As New DailyReportEntitiesEX
                '用Linq在数据实体中获取当前用用户的在日期区间内的所有审核资料，将会获得若干个Check实体（Model列表），然后再赋值给ocl
                ocl = (From c In re.Check
                      Where c.userid = userid And c.date >= startDate And c.date <= endDate
                      Select c).ToList
            End Using

            '先根据业务列表循环创建吉喈UserCheckReportModel
            For Each b In DailyReportCommon.GetBusinessList.BusinessList
                ucri = New UserCheckReportItem With {.Business = b.Name}
                ucrm.UserCheckReportItems.Add(ucri)
            Next

            '循环原始数据，讲相应的值塞给页面真正使用的Model中
            For Each c In ocl
                Dim tq = From f In ucrm.UserCheckReportItems
                             Where f.Business = c.business
                             Select f

                If tq.Count > 0 Then
                    ucri = tq.First
                    Select Case c.checklevel
                        Case 1
                            ucri.Level1 += c.checkcount
                        Case 2
                            ucri.Level2 += c.checkcount
                        Case 3
                            ucri.Level3 += c.checkcount
                        Case 4
                            ucri.Level4 += c.checkcount
                        Case 5
                            ucri.Level5 += c.checkcount
                    End Select
                Else
                    ucri = New UserCheckReportItem
                    ucri.Business = c.business
                    Select Case c.checklevel
                        Case 1
                            ucri.Level1 += c.checkcount
                        Case 2
                            ucri.Level2 += c.checkcount
                        Case 3
                            ucri.Level3 += c.checkcount
                        Case 4
                            ucri.Level4 += c.checkcount
                        Case 5
                            ucri.Level5 += c.checkcount
                    End Select

                    ucrm.UserCheckReportItems.Add(ucri)
                End If
            Next

            Return View(ucrm)
        End Function

        <Authorize()>
        Public Function GetCheckListStore(ByVal userid As String, ByVal checkdate As Date) As AjaxStoreResult
            Try
                Dim dclm As DayCheckListModel = getListModel(userid, checkdate)

                Return New AjaxStoreResult(dclm.DayCheckListItems, dclm.DayCheckListItems.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        Private Function getListModel(ByVal userid As String, ByVal checkdate As Date) As DayCheckListModel
            Dim startDate As Date
            Dim endDate As Date

            'If Now.Day <= 27 Then
            '    startDate = New Date(checkdate.Year, checkdate.Month - 1, 28)
            '    endDate = Now
            'Else
            '    startDate = New Date(checkdate.Year, Now.Month, 28)
            '    endDate = Now
            'End If

            Dim inDate As Date = Now

            If inDate.Day <= 28 Then
                If inDate.Month = 3 Then
                    If (Date.Parse(inDate.ToString("yyyy-MM") + "-1")).AddDays(-1).Day = 29 Then
                        startDate = New Date(inDate.Year, inDate.AddMonths(-1).Month, 29)
                    Else
                        startDate = New Date(inDate.Year, inDate.Month, 1)
                    End If
                Else
                    startDate = Date.Parse(inDate.AddMonths(-1).ToString("yyyy-MM") + "-29")
                End If
                endDate = Date.Parse(inDate.ToString("yyyy-MM") + "-28")
            Else
                startDate = Date.Parse(inDate.ToString("yyyy-MM") + "-29")
                endDate = Date.Parse(inDate.AddMonths(1).ToString("yyyy-MM") + "-28")
            End If

            Dim ocl As List(Of Check)
            Using re As New DailyReportEntitiesEX
                ocl = (
                    From c In re.Check
                    Where c.userid = userid And c.date >= startDate And c.date <= endDate
                    Select c).ToList
            End Using

            Dim dclm As New DayCheckListModel
            Dim dcli As DayCheckListItem
            For Each c In ocl
                Dim tq = From f In dclm.DayCheckListItems
                         Where f.CheckDate = c.date
                         Take 1
                         Select f

                If tq.Count > 0 Then
                    dcli = tq.First
                    Select Case c.checklevel
                        Case 1
                            dcli.Level1 += c.checkcount
                        Case 2
                            dcli.Level2 += c.checkcount
                        Case 3
                            dcli.Level3 += c.checkcount
                        Case 4
                            dcli.Level4 += c.checkcount
                        Case 5
                            dcli.Level5 += c.checkcount
                    End Select
                    dcli.Total += c.checkcount
                Else
                    dcli = New DayCheckListItem
                    dcli.CheckDate = c.date.ToString("yyyy-MM-dd")
                    Select Case c.checklevel
                        Case 1
                            dcli.Level1 = c.checkcount
                        Case 2
                            dcli.Level2 = c.checkcount
                        Case 3
                            dcli.Level3 = c.checkcount
                        Case 4
                            dcli.Level4 = c.checkcount
                        Case 5
                            dcli.Level5 = c.checkcount
                    End Select
                    dcli.Total += c.checkcount
                    'dcli.ID = c.id
                    dcli.UserID = c.userid
                    dclm.DayCheckListItems.Add(dcli)
                End If
            Next

            Return dclm

        End Function

        <Authorize()>
        Public Function GetCheckDetailStore(ByVal userid As String, ByVal checkdate As String) As AjaxStoreResult
            Try
                Dim ocl As List(Of Check)
                Using re As New DailyReportEntitiesEX
                    ocl = (
                        From c In re.Check
                        Where c.userid = userid And c.date = checkdate
                        Select c).ToList
                End Using

                Dim dcdm As New DayCheckDetailModel
                Dim dcdi As DayCheckDetailItem
                For Each c In ocl
                    Dim tq = From f In dcdm.DayCheckDetailItems
                             Where f.Business = c.business
                             Take 1
                             Select f

                    If tq.Count > 0 Then
                        dcdi = tq.Single
                        Select Case c.checklevel
                            Case 1
                                dcdi.Level1 += c.checkcount
                            Case 2
                                dcdi.Level2 += c.checkcount
                            Case 3
                                dcdi.Level3 += c.checkcount
                            Case 4
                                dcdi.Level4 += c.checkcount
                            Case 5
                                dcdi.Level5 += c.checkcount
                        End Select
                        dcdi.Total += c.checkcount
                        dcdi.Urgent += c.urgent
                        dcdi.Crossregion += c.crossregion
                    Else
                        dcdi = New DayCheckDetailItem
                        dcdi.CheckDate = c.date.ToString("yyyy-MM-dd")
                        Select Case c.checklevel
                            Case 1
                                dcdi.Level1 = c.checkcount
                            Case 2
                                dcdi.Level2 = c.checkcount
                            Case 3
                                dcdi.Level3 = c.checkcount
                            Case 4
                                dcdi.Level4 = c.checkcount
                            Case 5
                                dcdi.Level5 = c.checkcount
                        End Select
                        dcdi.Total += c.checkcount
                        dcdi.Urgent += c.urgent
                        dcdi.Crossregion += c.crossregion
                        'dcdi.ID = c.id
                        dcdi.UserID = c.userid
                        dcdi.Business = c.business
                        dcdm.DayCheckDetailItems.Add(dcdi)
                    End If
                Next
                Return New AjaxStoreResult(dcdm.DayCheckDetailItems, dcdm.DayCheckDetailItems.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        Public Function SaveDayCheckWithConfirmation() As AjaxStoreResult
            Dim ajaxStoreResult As New AjaxStoreResult(StoreResponseFormat.Save)
            Try
                Dim dataHandler = New StoreDataHandler(HttpContext.Request("data"))
                Dim data As ChangeRecords(Of DayCheckDetailItem) = dataHandler.ObjectData(Of DayCheckDetailItem)()
                Dim confirmationList As ConfirmationList = dataHandler.BuildConfirmationList("Business")

                For Each cdc In data.Created
                    Using re As New DailyReportEntitiesEX
                        writeDay(re, cdc, 1)
                        writeDay(re, cdc, 2)
                        writeDay(re, cdc, 3)
                        writeDay(re, cdc, 4)
                        writeDay(re, cdc, 5)
                        writeDayExtend(re, cdc)
                        re.SaveChanges()
                    End Using
                Next

                For Each udc In data.Updated
                    Using re As New DailyReportEntitiesEX

                        writeDay(re, udc, 1)
                        writeDay(re, udc, 2)
                        writeDay(re, udc, 3)
                        writeDay(re, udc, 4)
                        writeDay(re, udc, 5)
                        writeDayExtend(re, udc)
                        re.SaveChanges()
                    End Using
                Next

                For Each c In confirmationList
                    confirmationList(c.Key).ConfirmRecord()
                Next

                ajaxStoreResult.SaveResponse.ConfirmationList = confirmationList
            Catch ex As Exception
                ajaxStoreResult.SaveResponse.Success = False
                ajaxStoreResult.SaveResponse.Message = ex.Message
            End Try

            Return ajaxStoreResult
        End Function

        Private Sub writeDay(ByRef re As DailyReportEntitiesEX, ByVal dcdi As DayCheckDetailItem, ByVal level As Integer)
            Dim q = (From c In re.Check
                      Where c.userid = dcdi.UserID And c.date = dcdi.CheckDate And c.business = dcdi.Business And c.checklevel = level
                      Select c)

            Dim levelCount As Integer = 0
            Select Case level
                Case 1
                    levelCount = dcdi.Level1
                Case 2
                    levelCount = dcdi.Level2
                Case 3
                    levelCount = dcdi.Level3
                Case 4
                    levelCount = dcdi.Level4
                Case 5
                    levelCount = dcdi.Level5
            End Select

            If q.Count > 0 Then
                q.First().checkcount = levelCount
            Else
                If levelCount > 0 Then
                    Dim nc As New Check With {.id = System.Guid.NewGuid.ToString(),
                                              .business = dcdi.Business,
                                              .checklevel = level,
                                              .checkcount = levelCount,
                                              .userid = CurrentUserID,
                                              .date = Now}
                    re.Check.AddObject(nc)
                End If
            End If
        End Sub

        Private Sub writeDayExtend(ByRef re As DailyReportEntitiesEX, ByVal dcdi As DayCheckDetailItem)
            Dim q = (From c In re.Check
                      Where c.userid = dcdi.UserID And c.date = dcdi.CheckDate And c.business = dcdi.Business And c.checklevel = 0
                      Select c)
            Dim chechTotal = dcdi.Level1 + dcdi.Level2 + dcdi.Level3 + dcdi.Level4 + dcdi.Level5

            If q.Count > 0 Then
                With q.First
                    .checkcount = chechTotal
                    .urgent = dcdi.Urgent
                    .crossregion = dcdi.Crossregion
                End With
            Else
                Dim nc As New Check With {.id = System.Guid.NewGuid.ToString(),
                                          .business = dcdi.Business,
                                          .userid = CurrentUserID,
                                          .checkcount = chechTotal,
                                          .urgent = dcdi.Urgent,
                                          .crossregion = dcdi.Crossregion,
                                          .date = Now}
                re.Check.AddObject(nc)
            End If
        End Sub

#Region "饼"
        <Authorize()>
        Function WorkloadProportionChart(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer) As ActionResult
            Try
                ViewData("Level") = level
                Return View(GetWorkloadProportionData(rangestart, rangeend, level))
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Authorize()>
        Public Function WorkloadProportionStore(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer) As AjaxStoreResult
            Try
                Dim wpm As WorkloadProportionModel = GetWorkloadProportionData(rangestart, rangeend, level)
                wpm.RangeStart = rangestart
                wpm.RangeEnd = rangeend
                Dim wpril As New List(Of WorkloadProportionItem)

                wpril = wpm.ChartItems

                Return New AjaxStoreResult(wpril, wpril.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        'todo:加级数参数
        Private Function GetWorkloadProportionData(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer) As WorkloadProportionModel
            Dim wpcm As New WorkloadProportionModel
            Try
                'Dim wp As New WorkloadProportionModel
                Dim wpi As WorkloadProportionItem
                'Dim wpri As WorkloadProportionReportItem
                Dim mocl As New List(Of Check)

                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend
                '月 显示上个月的情况


                'If Now.Day <= 27 Then
                '    startDate = Date.Parse(Now.AddMonths(-1).ToString("yyyy-MM") + "-27")
                '    endDate = Date.Parse(Now.AddMonths(0).ToString("yyyy-MM") + "-28")
                'Else
                '    startDate = Date.Parse(Now.AddMonths(0).ToString("yyyy-MM") + "-27")
                '    endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-28")
                'End If

                Using re As New DailyReportEntitiesEX
                    '根据月区间查处原始数据
                    If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then


                        mocl = (From c In re.Check
                              Where (From u In re.User
                                     Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                     Select u.id).Contains(c.userid) And c.date >= startDate And c.date <= endDate And c.checklevel = level
                              Select c).ToList
                    Else
                        mocl = (From c In re.Check
                              Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startDate And c.date <= endDate And c.checklevel = level
                              Select c).ToList
                    End If
                End Using

                Dim bl As Businesses = DailyReportCommon.GetBusinessList()

                '月--ViewModel
                For Each m In mocl
                    For Each b In bl.BusinessList
                        If m.business = b.Name Then
                            Dim tq = From t In wpcm.ChartItems
                                     Where t.Business = m.business
                                     Select t
                            If tq.Count > 0 Then
                                wpi = tq.First
                                wpi.Count += m.checkcount
                            Else
                                wpi = New WorkloadProportionItem
                                wpi.Business = m.business
                                wpi.Count = m.checkcount
                                wpcm.ChartItems.Add(wpi)
                            End If
                        End If
                    Next
                Next

                wpcm.RangeStart = rangestart
                wpcm.RangeEnd = rangeend

                Return wpcm
            Catch ex As Exception
                Return wpcm
            End Try
        End Function
#End Region

#Region "走势"
        <Authorize()>
        Function WorkloadTrendsChart(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer, ByVal grain As ReportGrain) As ActionResult
            Try
                ViewData("Level") = level
                Return View(GetWorkloadTrendsData(rangestart, rangeend, level, Grain))
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Authorize()>
        Public Function WorkloadTrendsStore(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer, ByVal grain As ReportGrain) As AjaxStoreResult
            Dim wtcm As New WorkloadTrendsCharViewModel
            Try
                Dim wt As WorkloadTrends
                Dim wti As WorkloadTrendsItem
                Dim ocl As New List(Of Check)
                Dim bs As Businesses = DailyReportCommon.GetBusinessList
                wtcm.Businesses = bs

                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend

                Dim startMonthStart As New Date(startDate.Year, startDate.Month, 1)
                Dim endMonthStart As New Date(endDate.Year, endDate.Month, 1)
                Dim endMonthEnd As Date = endMonthStart.AddMonths(1).AddDays(-1)

                '先撞见一个吉喈ArrayList，作为最终输出Store
                Dim al As New ArrayList()

                Select grain
                    Case ReportGrain.Day
                        Using re As New DailyReportEntitiesEX
                            If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then

                                '根据月区间查处原始数据
                                ocl = (From c In re.Check
                                      Where (From u In re.User
                                             Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                             Select u.id).Contains(c.userid) And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                      Select c).ToList
                            Else
                                ocl = (From c In re.Check
                                      Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                      Select c).ToList
                            End If
                        End Using

                        'For Each m In ocl
                        '    Dim tq = From t In wtcm.ChartItems
                        '             Where t.CheckDate = m.date
                        '             Select t
                        '    '先找日期相同喈趋势model
                        '    If tq.Count > 0 Then
                        '        wt = tq.First

                        '        '然后找对应的Business
                        '        Dim tq2 = From t In wt.WorkloadTrendsItems
                        '             Where t.Business = m.business
                        '             Select t

                        '        If tq2.Count > 0 Then
                        '            wti = tq2.First
                        '            wti.Count += m.checkcount
                        '        Else
                        '            wti = New WorkloadTrendsItem
                        '            wti.Business = m.business
                        '            wti.Count = m.checkcount
                        '            wt.WorkloadTrendsItems.Add(wti)
                        '        End If

                        '    End If
                        'Next

                        '根据用户名同Business，创建吉数
                        Dim njo As New JsonObject
                        For d As Integer = 0 To (rangeend - rangestart).TotalDays
                            njo = New JsonObject
                            njo.Add("CheckDate", rangestart.AddDays(d).ToString("yyyy年MM月dd日"))
                            njo.Add("dCheckDate", rangestart.AddDays(d))

                            For Each bb In bs.BusinessList
                                njo.Add(bb.Value, 0)
                            Next
                            al.Add(njo)
                        Next

                        '找数据库中的数据，找到对应的用户名用business，然后填到报表中
                        For Each m In ocl
                            For Each jo As JsonObject In al
                                If jo("dCheckDate") = m.date Then
                                    Dim q = (From t In bs.BusinessList
                                              Where t.Name = m.business)
                                    If q.Count > 0 Then
                                        jo(q.First.Value) += m.checkcount
                                    End If
                                End If
                            Next
                        Next


                    Case ReportGrain.Month
                        Dim njo As JsonObject
                        Dim tm = startMonthStart
                        Do While tm <= endMonthStart
                            njo = New JsonObject
                            njo.Add("CheckDate", tm.ToString("yyyy年MM月"))
                            njo.Add("dCheckDate", tm)

                            For Each b In bs.BusinessList
                                njo.Add(b.Value, 0)
                            Next
                            tm = tm.AddMonths(1)
                            al.Add(njo)
                        Loop

                        Using re As New DailyReportEntitiesEX
                            If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then
                                ocl = (From c In re.Check
                                      Where (From u In re.User
                                             Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                             Select u.id).Contains(c.userid) And c.date >= startMonthStart And c.date <= endMonthEnd And c.checklevel = level
                                      Select c).ToList
                            Else
                                ocl = (From c In re.Check
                                        Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startMonthStart And c.date <= endMonthEnd And c.checklevel = level
                                        Select c).ToList
                            End If
                        End Using

                        'For Each a In al
                        '    Dim currentMonthStart As Date = a("dCheckDate")
                        '    Dim currentMonthEnd As Date = currentMonthStart.AddMonths(1).AddDays(-1)
                        '    Dim tq = From m In ocl
                        '             Where m.date >= currentMonthStart And m.date <= currentMonthEnd
                        '             Select m
                        '    '先找日期相同喈趋势model
                        '    If tq.Count > 0 Then
                        '        For Each q In tq
                        '            Dim tq2 = From t In wt.WorkloadTrendsItems
                        '                      Where t.Business = q.business
                        '                      Select t

                        '            If tq2.Count > 0 Then
                        '                wti = tq2.First
                        '                wti.Count += q.checkcount
                        '            Else
                        '                wti = New WorkloadTrendsItem
                        '                wti.Business = q.business
                        '                wti.Count = q.checkcount
                        '                wt.WorkloadTrendsItems.Add(wti)
                        '            End If
                        '        Next
                        '    End If
                        'Next

                        

                        For Each m In ocl
                            For Each jo As JsonObject In al
                                Dim currentMonthStart As Date = jo("dCheckDate")
                                Dim currentMonthEnd As Date = currentMonthStart.AddMonths(1).AddDays(-1)

                                If m.date >= currentMonthStart And m.date <= currentMonthEnd Then
                                    Dim q = (From t In bs.BusinessList
                                              Where t.Name = m.business)
                                    If q.Count > 0 Then
                                        jo(q.First.Value) += m.checkcount
                                    End If
                                End If
                            Next
                        Next
                End Select

                wtcm.RangeStart = rangestart
                wtcm.RangeEnd = rangeend

                Return New AjaxStoreResult(al, al.Count)

                'Return wtcm
            Catch ex As Exception
                Return Nothing 'wtcm
            End Try
        End Function

        Private Function GetWorkloadTrendsData(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer, ByVal grain As ReportGrain) As WorkloadTrendsCharViewModel
            Dim wtcm As New WorkloadTrendsCharViewModel
            Try
                Dim wt As WorkloadTrends
                Dim wti As WorkloadTrendsItem
                Dim mocl As New List(Of Check)
                Dim bs As Businesses = DailyReportCommon.GetBusinessList
                wtcm.Businesses = bs

                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend

                Dim startMonthStart As New Date(startDate.Year, startDate.Month, 1)
                Dim endMonthStart As New Date(endDate.Year, endDate.Month, 1)
                Dim endMonthEnd As Date = endMonthStart.AddMonths(1).AddDays(-1)
                '月 显示上个月的情况

                Select Case Grain
                    Case ReportGrain.Day
                        '先插吉数
                        For i = 0 To (endDate - startDate).Days
                            wt = New WorkloadTrends
                            wt.CheckDate = startDate.AddDays(i)
                            For Each b In bs.BusinessList
                                wti = New WorkloadTrendsItem With {.Business = b.Name, .Count = 0}
                                wt.WorkloadTrendsItems.Add(wti)
                            Next
                            wtcm.ChartItems.Add(wt)
                        Next

                        Using re As New DailyReportEntitiesEX
                            If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then

                                '根据月区间查处原始数据
                                mocl = (From c In re.Check
                                      Where (From u In re.User
                                             Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                             Select u.id).Contains(c.userid) And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                      Select c).ToList
                            Else
                                mocl = (From c In re.Check
                                        Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                        Select c).ToList
                            End If
                        End Using

                        For Each m In mocl
                            Dim tq = From t In wtcm.ChartItems
                                     Where t.CheckDate = m.date
                                     Select t
                            '先找日期相同喈趋势model
                            If tq.Count > 0 Then
                                wt = tq.First

                                '然后找对应的Business
                                Dim tq2 = From t In wt.WorkloadTrendsItems
                                     Where t.Business = m.business
                                     Select t

                                If tq2.Count > 0 Then
                                    wti = tq2.First
                                    wti.Count += m.checkcount
                                Else
                                    wti = New WorkloadTrendsItem
                                    wti.Business = m.business
                                    wti.Count = m.checkcount
                                    wt.WorkloadTrendsItems.Add(wti)
                                End If

                            End If
                        Next
                    Case ReportGrain.Month
                        '先获取开始月份以及结束月份
                        Dim tm = startMonthStart
                        Do While tm <= endMonthStart
                            wt = New WorkloadTrends
                            wt.CheckDate = tm
                            For Each b In bs.BusinessList
                                wti = New WorkloadTrendsItem With {.Business = b.Name, .Count = 0}
                                wt.WorkloadTrendsItems.Add(wti)
                            Next
                            wtcm.ChartItems.Add(wt)
                            tm = tm.AddMonths(1)
                        Loop

                        Using re As New DailyReportEntitiesEX
                            If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then
                                mocl = (From c In re.Check
                                      Where (From u In re.User
                                             Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                             Select u.id).Contains(c.userid) And c.date >= startMonthStart And c.date <= endMonthEnd And c.checklevel = level
                                      Select c).ToList
                            Else
                                mocl = (From c In re.Check
                                        Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startMonthStart And c.date <= endMonthEnd And c.checklevel = level
                                        Select c).ToList
                            End If
                        End Using

                        For Each wt In wtcm.ChartItems
                            Dim currentMonthStart As Date = Date.Parse(wt.CheckDate)
                            Dim currentMonthEnd As Date = currentMonthStart.AddMonths(1).AddDays(-1)
                            Dim tq = From m In mocl
                                     Where m.date >= wt.CheckDate And m.date <= currentMonthEnd
                                     Select m
                            '先找日期相同喈趋势model
                            If tq.Count > 0 Then
                                For Each q In tq
                                    Dim tq2 = From t In wt.WorkloadTrendsItems
                                    Where t.Business = q.business
                                    Select t

                                    If tq2.Count > 0 Then
                                        wti = tq2.First
                                        wti.Count += q.checkcount
                                    Else
                                        wti = New WorkloadTrendsItem
                                        wti.Business = q.business
                                        wti.Count = q.checkcount
                                        wt.WorkloadTrendsItems.Add(wti)
                                    End If
                                Next
                            End If
                        Next
                End Select

                Select Case grain
                    Case ReportGrain.Day
                        wtcm.RangeStart = rangestart
                        wtcm.RangeEnd = rangeend
                    Case ReportGrain.Month
                        wtcm.RangeStart = startMonthStart
                        wtcm.RangeEnd = endMonthStart '.AddDays(-1)
                End Select

                wtcm.Grain = Grain
                Return wtcm
            Catch ex As Exception
                Return wtcm
            End Try
        End Function
#End Region

#Region "业务柱"
        <Authorize()>
        Function BusinessChart(ByVal rangestart As Date, ByVal rangeend As Date, ByVal businessname As String, ByVal level As Integer) As ActionResult
            Try
                ViewData("Level") = level
                Return View(GetBusinessChartData(rangestart, rangeend, businessname, level))
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        'todo:加级数参数
        Private Function GetBusinessChartData(ByVal rangestart As Date, ByVal rangeend As Date, ByVal businessname As String, ByVal level As Integer) As BusinessChartModel
            Dim bcm As New BusinessChartModel
            Try
                Dim users As New List(Of User)
                Dim mocl As New List(Of Check)
                Dim bci As BusinessChartItem

                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend
                '月 显示上个月的情况

                'If Now.Day <= 27 Then
                '    startDate = Date.Parse(Now.AddMonths(-1).ToString("yyyy-MM") + "-27")
                '    endDate = Date.Parse(Now.AddMonths(0).ToString("yyyy-MM") + "-28")
                'Else
                '    startDate = Date.Parse(Now.AddMonths(0).ToString("yyyy-MM") + "-27")
                '    endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-28")
                'End If

                Using re As New DailyReportEntitiesEX
                    If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then

                        users = (From u In re.User
                                 Where u.departmentid = DailyReportCommon.CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                 Select u).ToList

                        mocl = (From c In re.Check
                              Where (From u In re.User
                                     Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                     Select u.id).Contains(c.userid) And c.business = businessname And c.date >= startDate And c.date <= endDate And c.checklevel = level
                              Select c).ToList
                    Else
                        users = (From u In re.User
                                 Where u.id = DailyReportCommon.CurrentUserID And u.status = DailyReportCommon.UserStatus.正常
                                 Select u).ToList

                        mocl = (From c In re.Check
                              Where c.userid = DailyReportCommon.CurrentUserID And c.business = businessname And c.date >= startDate And c.date <= endDate And c.checklevel = level
                              Select c).ToList
                    End If
                End Using

                For Each u In users
                    bci = New BusinessChartItem With {.UserName = u.username, .UserID = u.id, .Count = 0}
                    bcm.ChartItems.Add(bci)
                Next

                For Each m In mocl
                    Dim tq = From t In bcm.ChartItems
                             Where t.UserID = m.userid
                             Select t

                    If tq.Count > 0 Then
                        bci = tq.First
                        bci.Count += m.checkcount
                    End If
                Next

                bcm.BusinessName = businessname
                bcm.RangeStart = rangestart
                bcm.RangeEnd = rangeend
                Return bcm
            Catch ex As Exception
                Return bcm
            End Try
        End Function
#End Region

#Region "部门审核报表"
        <Authorize()>
        Function DepartmentCheckReport() As ActionResult
            ViewData("UserID") = CurrentUserID
            ViewData("CheckDate") = Now.ToString("yyyy-MM-dd")
            ViewData("Level") = 1

            Dim dcrbbm As New DepartmentCheckReportByBusinessModel
            dcrbbm.Businesses = DailyReportCommon.GetBusinessList

            Return View(dcrbbm)
        End Function

        'todo:加级数参数
        <Authorize()>
        Public Function GetDepartmentCheckListByBusinessStore(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer) As AjaxStoreResult
            Try

                '用个暂时唯一喈本办法去处理动态列数据喈问题
                '由于列系动态，所以唔可能用固定喈Model去处理，所以只有用一个ArrayList
                '然后没一行数据根据Business动态去取出每一个人对应喈Business喈一审数量填入去

                Dim ocl As New List(Of Check)
                Dim ul As New List(Of User)

                '获取Business列表
                Dim bs As Businesses = DailyReportCommon.GetBusinessList

                '定义时间区间
                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend
                'Select Case range
                '    Case 1 '月
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 2 '季
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 3 '年
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                'End Select

                Using re As New DailyReportEntitiesEX
                    '根据月区间查处原始数据
                    If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then
                        If level > 0 Then
                            ocl = (From c In re.Check
                                   Where (From u In re.User
                                           Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                           Select u.id).Contains(c.userid) And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                   Select c).ToList
                        Else
                            ocl = (From c In re.Check
                                   Where (From u In re.User
                                           Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                           Select u.id).Contains(c.userid) And c.date >= startDate And c.date <= endDate
                                   Select c).ToList
                        End If

                        ul = (From u In re.User
                              Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                              Select u).ToList
                    Else    '如果无权限就只可以睇自己
                        If level > 0 Then
                            ocl = (From c In re.Check
                                   Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startDate And c.date <= endDate And c.checklevel = level
                                   Select c).ToList
                        Else
                            ocl = (From c In re.Check
                                   Where c.userid = DailyReportCommon.CurrentUserID And c.date >= startDate And c.date <= endDate
                                   Select c).ToList
                        End If

                        ul = (From u In re.User
                              Where u.id = DailyReportCommon.CurrentUserID
                              Select u).ToList
                    End If
                End Using

                '先撞见一个吉喈ArrayList
                Dim al As New ArrayList()

                '根据用户名同Business，创建吉数
                Dim njo As New JsonObject
                Dim i As Integer
                For Each u In ul
                    njo = New JsonObject
                    njo.Add("UserName", u.username)
                    njo.Add("UserID", u.id)

                    For Each b In bs.BusinessList
                        njo.Add(b.Value, 0)
                    Next
                    al.Add(njo)
                Next

                '找数据库中的数据，找到对应的用户名用business，然后填到报表中
                For Each m In ocl
                    For Each jo As JsonObject In al
                        If jo("UserID") = m.userid Then
                            Dim q = (From t In bs.BusinessList
                                      Where t.Name = m.business)
                            If q.Count > 0 Then
                                jo(q.First.Value) += m.checkcount
                            End If
                        End If
                    Next
                Next

                Return New AjaxStoreResult(al, al.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Authorize()>
        Public Function GetDepartmentCheckListByLevelStore(ByVal rangestart As Date, ByVal rangeend As Date, ByVal level As Integer) As AjaxStoreResult
            Try
                'Dim pm As ParmissionsModel = DailyReportCommon.GetMaskList()

                'Dim mp = DailyReportCommon.MatchingPermission(

                Dim mp = DailyReportCommon.MatchingPermission(CurrentUserPermissions, 4)

                Dim dcrblm As New DepartmentCheckReportByLevelModel
                Dim dcrbli As DepartmentCheckReportByLevelItem

                Dim startDate As Date = rangestart
                Dim endDate As Date = rangeend
                'Select Case range
                '    Case 1 '月
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 2 '季
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 3 '年
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                'End Select

                Dim ocl As New List(Of Check)
                Dim dul As New List(Of User)
                Dim du As User
                Using re As New DailyReportEntitiesEX
                    If DailyReportCommon.MatchingPermission(DailyReportCommon.CurrentUserPermissions, DailyReportCommon.PermissionList.审核报表) Then
                        dul = (From u In re.User
                               Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                               Select u).ToList
                        'If dul.Count > 0 Then
                        '    Dim dc
                        'For Each u In dul
                        '    dc = (From d In re.Check
                        '          Where d.userid = u.id And d.date >= startDate And d.date <= endDate
                        '          Select d)
                        '    For Each c In dc
                        '        ocl.Add(c)
                        '    Next
                        'Next
                        ocl = (From d In re.Check
                              Where (From u In re.User
                                     Where u.departmentid = CurrentDepartmentID And u.status = DailyReportCommon.UserStatus.正常
                                     Select u.id).Contains(d.userid) And d.date >= startDate And d.date <= endDate
                              Select d).ToList

                        'End If
                    Else
                        dul = (From u In re.User
                               Where u.id = DailyReportCommon.CurrentUserID And u.status = DailyReportCommon.UserStatus.正常
                               Select u).ToList

                        ocl = (From d In re.Check
                              Where d.userid = DailyReportCommon.CurrentUserID And d.date >= startDate And d.date <= endDate
                              Select d).ToList
                    End If
                End Using

                For Each c In ocl
                    Dim tq = From f In dcrblm.DayCheckListItemsByLevel
                             Where f.UserID = c.userid
                             Select f

                    If tq.Count > 0 Then
                        dcrbli = tq.First
                        Select Case c.checklevel
                            Case 1
                                dcrbli.Level1 += c.checkcount
                            Case 2
                                dcrbli.Level2 += c.checkcount
                            Case 3
                                dcrbli.Level3 += c.checkcount
                            Case 4
                                dcrbli.Level4 += c.checkcount
                            Case 5
                                dcrbli.Level5 += c.checkcount
                        End Select
                        dcrbli.Total += c.checkcount
                    Else
                        du = (From u In dul
                             Where u.id = c.userid
                             Take 1
                             Select u).First

                        dcrbli = New DepartmentCheckReportByLevelItem
                        dcrbli.UserID = du.id
                        dcrbli.UserName = du.username
                        Select Case c.checklevel
                            Case 1
                                dcrbli.Level1 = c.checkcount
                            Case 2
                                dcrbli.Level2 = c.checkcount
                            Case 3
                                dcrbli.Level3 = c.checkcount
                            Case 4
                                dcrbli.Level4 = c.checkcount
                            Case 5
                                dcrbli.Level5 = c.checkcount
                        End Select
                        dcrbli.Total += c.checkcount
                        dcrblm.DayCheckListItemsByLevel.Add(dcrbli)
                    End If
                Next

                Return New AjaxStoreResult(dcrblm.DayCheckListItemsByLevel, dcrblm.DayCheckListItemsByLevel.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
#End Region


    End Class
End Namespace